package com.cheng.lesson02;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//客户端
public class TCPClientDemon01 {
    public static void main(String[] args) {
        Socket socket =null;
        OutputStream os =null;

        try {
            //1.获取服务端的地址和端口号
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");
            int port=7777;
            //2.创建一个socket连接
             socket = new Socket(serverIp,7777);
            //3.用IO流发送消息
             os = socket.getOutputStream();
            //4.编写发送的内容
            os.write("万里顾一程,学习".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
